﻿using LPY.LeHealth.DTO;
using LPY.LeHealth.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LPY.LeHealth.WebSite.Common
{
    public class UserAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
                var currentUser = filterContext.HttpContext.Session[CommonService.UserSessionName] as UserDTO;
                if (null == currentUser)
                {
                    if (filterContext.HttpContext.Request.IsAjaxRequest())
                    {
                        //ajax请求，返回Json
                        var result = new JsonResult();
                        result.Data =new Common.JsonMessage(){ Code = 0, Msg = "未登录" };
                        filterContext.Result = result;
                    }
                    else
                    {
                        //记录登录前的Url地址
                        string returnUrl = filterContext.HttpContext.Request.Url.AbsolutePath;
                        string redirectUrl = string.Format("?ReturnUrl={0}", returnUrl);

                        //跳转到登录页面				
                        filterContext.Result = new RedirectResult("/User/Login" + redirectUrl);
                    }
                }
            
        }
    }
}